﻿#pragma execution_character_set("utf-8")
#include "reporter.h"
#include "ui_reporter.h"
#include <QFile>
#include <QTextStream>

Reporter::Reporter(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Reporter)
{
    ui->setupUi(this);
    model = NULL;
    this->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint);

    ui->pushButton_saveas->setVisible(false);
}

void Reporter::CreateTestReport(const SetupCase &setupCase, const QList<UdpDevice >  &udpDeviceList, const QList<TestCase> &testCaseList)
{
    locked = true;
    this->setVisible(true);
    ui->label_status->setText("状态：测试报告保存中，请稍候...");
    ui->label_status->setStyleSheet("color: rgb(255, 0, 0); font: 14pt;");
    this->setEnabled(false);
    QCoreApplication::processEvents();

    QString headerName;
    if(model != NULL){
        model->clear();
        delete model;
    }
    ui->label_script->setText(setupCase.testFile);
    ui->label_startTime->setText(setupCase.startTime.toString("yyyy年M月d日 hh:mm:ss"));
    ui->label_endTime->setText(setupCase.endTime.toString("yyyy年M月d日 hh:mm:ss"));

    model = new QStandardItemModel;
    model->setColumnCount(testCaseList.length()*2+2);
    model->setHeaderData(0, Qt::Horizontal, "设备名");
    model->setHeaderData(1, Qt::Horizontal, "MAC地址");
    for(int i=0; i<testCaseList.length(); i++){
        headerName = testCaseList.at(i).name;
        if(headerName.length() > 7)
            headerName.insert(7, '\n');
        headerName.append(QString("：%1次").arg(testCaseList.at(i).counts));
        headerName.append(QString("\n(单位: %1)").arg(testCaseList.at(i).unitStr));
        model->setHeaderData(i*2+2, Qt::Horizontal, headerName);
        model->setHeaderData(i*2+3, Qt::Horizontal, "时间");
    }
    ui->tableView->setModel(model);
    QCoreApplication::processEvents();

    for(int i=0; i<udpDeviceList.length(); i++){
        model->setItem(i, 0, new QStandardItem(QString("%1-%2").arg(setupCase.devName).arg(i+1)));                                  //设备名称
        model->setItem(i, 1, new QStandardItem(QString("%1\n%2").arg(udpDeviceList.at(i).mac1).arg(udpDeviceList.at(i).mac2)));     //MAC地址
        for(int j=0; j<testCaseList.length(); j++){
            if(udpDeviceList.at(i).cmdIndex > j){
                QString resultStr = udpDeviceList.at(i).cmdResult.at(j);
                QStandardItem* standardItem_result = new QStandardItem(resultStr);
                QStandardItem* standardItem_time   = new QStandardItem(QString("%1s").arg(udpDeviceList.at(i).testTimes.at(j)));
                if(resultStr.contains("偏") || resultStr.contains("异常") || resultStr.contains("NULL")){
                    standardItem_result->setForeground(QBrush(QColor(255, 0, 0)));
                    standardItem_time->setForeground(QBrush(QColor(255, 0, 0)));
                }
                model->setItem(i, 2+j*2, standardItem_result);              //单项测试结果
                model->setItem(i, 3+j*2, standardItem_time);                //单项测试结果执行时间

            }else{
                model->setItem(i, 2+j*2, new QStandardItem(QString("%1").arg("--")));
                model->setItem(i, 3+j*2, new QStandardItem(QString("%1").arg("--")));
            }
        }
    }
    QCoreApplication::processEvents();
    ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();

    QString fileName = QString("./reports/%1-%2.csv").arg(setupCase.devName).arg(setupCase.startTime.toString("yyyy年M月d日-hh-mm-ss"));
    QFile file(fileName);
    if(file.open(QIODevice::WriteOnly)){
        QTextStream stream(&file);
        int cc = model->columnCount();
        QStringList list;
        for(int i=0; i<cc; i++){
            list << model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString().simplified();
        }
        stream << list.join(",")<<"\r\n";

        for(int i=0; i<model->rowCount(); i++){
            list.clear();
            for(int j=0; j<cc; j++){
                list << model->index(i,j).data().toString().simplified();
            }
            stream << list.join(",")<<"\r\n";
        }
        file.close();
    }

    ui->label_status->setText("状态：测试报告保存成功：" + fileName);
    ui->label_status->setStyleSheet("color: rgb(10, 10, 10); font: 14pt;");
    this->setEnabled(true);
    emit reportFinshed();
    locked = false;

    this->resizeEvent(NULL);
}

Reporter::~Reporter()
{
    delete ui;
}

void Reporter::resizeEvent(QResizeEvent *)
{
    if(locked)
        return;
    int counts = model->columnCount();          //获取表格的列数
    int widths = ui->tableView->width();
    float colWidthsAve = widths * 1.0 / counts;
    for(int i=0; i<counts; i++){
      ui->tableView->setColumnWidth(i, colWidthsAve);
    }
    //ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();
}

void Reporter::on_pushButton_exit_clicked()
{
    this->close();
}

void Reporter::on_pushButton_saveas_clicked()
{

}
